home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JDBC / JDBC_011 / JAVA / SQL / DATE.JAV < prev    next >
Encoding:
Text File  |  1996-11-10  |  4.6 KB  |  169 lines

  1. /*
  2.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "LICENSE"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  * 
  17.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  18.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  19.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  20.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  21.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  22.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  23.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  24.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  25.  * HIGH RISK ACTIVITIES.
  26.  */
  27.  
  28. package java.sql;
  29.  
  30. /**
  31.  * <P>This class is a thin wrapper around java.util.Date that allows
  32.  * JDBC to identify this as a SQL DATE value. It adds formatting and
  33.  * parsing operations to support the JDBC escape syntax for date
  34.  * values.
  35.  */
  36. public class Date extends java.util.Date {
  37.  
  38.     /**
  39.      * Construct a Date  
  40.      *
  41.      * @param year year-1900
  42.      * @param month 0 to 11 
  43.      * @param day 1 to 31
  44.      */
  45.     public Date(int year, int month, int day) {
  46.     super(year, month, day);
  47.     }
  48.  
  49.     /**
  50.      * Construct a Date using a milliseconds time value
  51.      *
  52.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT
  53.      */
  54.     public Date(long date) {
  55.     // If the millisecond date value contains time info, mask it out.
  56.     super(date);
  57.     int year = getYear();
  58.     int month = getMonth();
  59.     int day = getDate();
  60.     super.setTime(0);
  61.     setYear(year);
  62.     setMonth(month);
  63.     setDate(day);
  64.     }
  65.  
  66.     /**
  67.      * Set a Date using a milliseconds time value
  68.      *
  69.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT
  70.      */
  71.     public void setTime(long date) {
  72.     // If the millisecond date value contains time info, mask it out.
  73.     super.setTime(date);
  74.     int year = getYear();
  75.     int month = getMonth();
  76.     int day = getDate();
  77.     super.setTime(0);
  78.     setYear(year);
  79.     setMonth(month);
  80.     setDate(day);
  81.     }
  82.  
  83.     /**
  84.      * Convert a string in JDBC date escape format to a Date value
  85.      *
  86.      * @param s date in format "yyyy-mm-dd"
  87.      * @return corresponding Date
  88.      */
  89.     public static Date valueOf(String s) {
  90.     int year;
  91.     int month;
  92.     int day;
  93.     int firstDash;
  94.     int secondDash;
  95.  
  96.     if (s == null) throw new java.lang.IllegalArgumentException();
  97.  
  98.     firstDash = s.indexOf('-');
  99.     secondDash = s.indexOf('-', firstDash+1);
  100.     if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
  101.         year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
  102.         month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
  103.         day = Integer.parseInt(s.substring(secondDash+1));     
  104.     } else {
  105.         throw new java.lang.IllegalArgumentException();
  106.     }
  107.             
  108.     return new Date(year, month, day);
  109.     }
  110.  
  111.     /**
  112.      * Format a date in JDBC date escape format  
  113.      *
  114.      * @return a String in yyyy-mm-dd format
  115.      */
  116.     public String toString () {
  117.     int year = super.getYear() + 1900;
  118.     int month = super.getMonth() + 1;
  119.     int day = super.getDate();
  120.     String yearString;
  121.     String monthString;
  122.     String dayString;
  123.  
  124.         
  125.     yearString = Integer.toString(year);
  126.  
  127.     if (month < 10) {
  128.         monthString = "0" + month;
  129.     } else {        
  130.         monthString = Integer.toString(month);
  131.     }
  132.  
  133.     if (day < 10) {
  134.         dayString = "0" + day;
  135.     } else {        
  136.         dayString = Integer.toString(day);
  137.     }
  138.  
  139.     return ( yearString + "-" + monthString + "-" + dayString);
  140.     }
  141.  
  142.     // Override all the time operations inherited from java.util.Date;
  143.     public int getHours() {
  144.     throw new java.lang.IllegalArgumentException();
  145.     }
  146.  
  147.     public int getMinutes() {
  148.     throw new java.lang.IllegalArgumentException();
  149.     }
  150.     
  151.     public int getSeconds() {
  152.     throw new java.lang.IllegalArgumentException();
  153.     }
  154.  
  155.     public void setHours(int i) {
  156.     throw new java.lang.IllegalArgumentException();
  157.     }
  158.  
  159.     public void setMinutes(int i) {
  160.     throw new java.lang.IllegalArgumentException();
  161.     }
  162.  
  163.     public void setSeconds(int i) {
  164.     throw new java.lang.IllegalArgumentException();
  165.     }
  166.  
  167. }
  168.  
  169.